package com.maxiaowei.controller;

import com.maxiaowei.pojo.bo.ShopcartBO;
import com.maxiaowei.util.CustomJSONResult;
import com.maxiaowei.util.JsonUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 功能描述: 购物车相关接口
 * <p>
 * 作者: maxiaowei
 */
@RestController
@RequestMapping("/shopcart")
@Tag(name = "购物车接口controller", description = "购物车接口相关的api")
@Slf4j
public class ShopcartController extends BaseController {

    @Operation(summary = "添加商品到购物车", description = "添加商品到购物车", method = "POST")
    @PostMapping("/add")
    public CustomJSONResult add(@RequestParam String userId,
                                @RequestBody ShopcartBO shopcartBO,
                                HttpServletRequest request,
                                HttpServletResponse response) {
        if (StringUtils.isBlank(userId)) {
            return CustomJSONResult.errorMsg("");
        }
        log.info("shopcartBO:{}", JsonUtils.objectToJson(shopcartBO));
        // TODO 前端用户在登录的情况下，添加商品到购物车，会同时在后端同步购物车到redis缓存
        return CustomJSONResult.ok();
    }

    @Operation(summary = "从购物车中删除商品", description = "从购物车中删除商品", method = "POST")
    @PostMapping("/del")
    public CustomJSONResult del(@RequestParam String userId,
                                @RequestParam String itemSpecId,
                                HttpServletRequest request,
                                HttpServletResponse response) {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(itemSpecId)) {
            return CustomJSONResult.errorMsg("");
        }
        // TODO 用户在页面删除购物车中的商品数据，如果此时用户已经登录，则需要同步删除后端购物车中的商品数据
        return CustomJSONResult.ok();
    }
}
